package com.how2java;

import java.io.IOException;
import java.io.InputStream;
import java.util.List;

import com.how2java.mapper.CategoryMapper;
import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;

import com.how2java.pojo.Category;

public class TestMybatis {

  public static void main(String[] args) throws IOException {
    String resource = "mybatis-config.xml";
    InputStream inputStream = Resources.getResourceAsStream(resource);
    SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);
    SqlSession session = sqlSessionFactory.openSession();
    CategoryMapper mapper = session.getMapper(CategoryMapper.class);

//        add(mapper);
//        delete(mapper);
        get(mapper);
//        update(mapper);
//    listAll(mapper);

    session.commit();
    session.close();

  }

  private static void update(CategoryMapper mapper) {
    Category c= mapper.get(2);
    c.setName("修改了的Category名稱");
    mapper.update(c);
    listAll(mapper);
  }

  private static void get(CategoryMapper mapper) {
    Category c= mapper.get(2);
    System.out.println(c.getName());
  }

  private static void delete(CategoryMapper mapper) {
    mapper.delete(2);
    listAll(mapper);
  }

  private static void add(CategoryMapper mapper) {
    Category c = new Category();
    c.setName("新增加的Category");
    mapper.add(c);
    listAll(mapper);
  }

  private static void listAll(CategoryMapper mapper) {
    List<Category> cs = mapper.list();
    for (Category c : cs) {
      System.out.println(c.getName());
    }
  }
}
